//
//  Fraction.m
//  ClassAddObjectHomework
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction
- (void)output
{
    NSLog(@"The fraction is %ld / %ld",_numerator,_denominator);
}


- (void) reduce {
    NSInteger numerator = _numerator;
    NSInteger denominator = _denominator;
    NSInteger temp ;
    while ((temp = numerator % denominator) != 0) {
        numerator = denominator;
        denominator = temp;
    }
    _numerator /= denominator;
    _denominator /= denominator;
    
}


- (NSInteger) compareResult:(Fraction *)f
{
    
    NSInteger fenzi1 = 0;
    NSInteger fenzi2 = 0;
    
    fenzi1 =  self.numerator * f.denominator ;
    fenzi2 =  self.denominator *f.numerator;
    
    return (fenzi1 - fenzi2);
}

- (Fraction *) add:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = self.numerator * f.denominator + self.denominator * f.numerator;
    result.denominator = self.denominator * f.denominator;
    return  result;
}

- (Fraction *) sub:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = self.numerator * f.denominator - self.denominator * f.numerator;
    result.denominator = self.denominator * f.denominator;
    return  result;
}

- (Fraction *) mul:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = self.numerator * f.numerator;
    result.denominator = self.denominator * f.denominator;
    return result;
}

- (Fraction *) div:(Fraction *)f
{
    Fraction *result = [[Fraction alloc] init];
    result.numerator = self.numerator * f.denominator;
    result.denominator = self.denominator * f.numerator;
    return result;
}

- (instancetype)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator
{
    self = [super init];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
    }
    return self;
}



+ (instancetype)fractionWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator
{
    return [[Fraction alloc] initWithNumerator:numerator denominator:denominator];
}

@end
